package com.scoe.gcs.comms.activites;

import android.app.Activity;
import android.content.Intent;
import com.MAVLink.MAVLink;
import com.MAVLink.Messages.IMAVLinkMessage;
import com.MAVLink.Messages.common.msg_attitude;
import com.MAVLink.Messages.common.msg_gps_raw_int;
import com.MAVLink.Messages.common.msg_gps_status;
import com.MAVLink.Messages.common.msg_nav_controller_output;
import com.MAVLink.Messages.common.msg_request_data_stream;
import com.MAVLink.Messages.common.msg_sys_status;
import com.MAVLink.Messages.common.msg_waypoint_current;
import com.scoe.gcs.comms.AC1Data.FlightData;
import com.scoe.gcs.comms.AC1Data.ProtocolParser;
import com.scoe.gcs.comms.CommonSettings;
import com.scoe.gcs.comms.CommunicationClient;
import com.scoe.gcs.comms.ScoeGcsCommsModule;
import com.scoe.gcs.comms.CommsProxy;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;

public class HUDActivity{

	private Activity parent;
	
	private CommsProxy commsProxy = new CommsProxy();
	private KrollDict props = new KrollDict();
	
	private TiApplication appContext = TiApplication.getInstance();
	private Activity rootActivity = appContext.getCurrentActivity();
	
	public int hudHeight = 640, hudWidth = 476;
	public HUD hud = new HUD(rootActivity.getApplicationContext());
	public TiBlob blob;
	
	public HUDActivity(Activity p){
		parent = p;
		ba.init();
	}
	
	public void destroyComms(){
		ba.onDestroy();
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		ba.onActivityResult(requestCode, resultCode, data);
		
	}
	


	CommunicationClient ba = new CommunicationClient(rootActivity) {
		
		@Override
		public void notifyConnected() {
			if( CommonSettings.isProtocolAC1())
				sendBytesToComm(ProtocolParser.requestFlightData());
			else if (CommonSettings.isProtocolMAVLink()){
				
				msg_request_data_stream req = new msg_request_data_stream();
				//req.req_message_rate = 20;
				req.req_message_rate = 10;
				req.req_stream_id = MAVLink.MAV_DATA_STREAM.MAV_DATA_STREAM_EXTRA1; // MAV_DATA_STREAM_RAW_CONTROLLER;
																					// MAV_DATA_STREAM_RAW_SENSORS;
				req.start_stop = 1;
				req.target_system = MAVLink.CURRENT_SYSID;
				req.target_component = 0;
				ba.sendBytesToComm( MAVLink.createMessage(req));
				
				
				req = new msg_request_data_stream();
				req.req_message_rate = 1;
				req.req_stream_id = MAVLink.MAV_DATA_STREAM.MAV_DATA_STREAM_EXTENDED_STATUS; 
																							 
				req.start_stop = 1;
				req.target_system = MAVLink.CURRENT_SYSID;
				req.target_component = 0;
				ba.sendBytesToComm( MAVLink.createMessage(req));
				
			}
		}

		@Override
		public void notifyDisconnected() {

		}

		@Override
		public void notifyDeviceNotAvailable() {

		}

		@Override
		public void notifyReceivedData(int count, IMAVLinkMessage m) {
			if( CommonSettings.isProtocolAC1()){
				if( FlightData.class.isInstance(m)){
					FlightData fd = (FlightData)m;
//					double g[] = {fd.gyroX, fd.gyroY, fd.gyroYaw};
					double a[] = {fd.accelX, fd.accelY, fd.accelZ};
//					double mag[] = {fd.magX, fd.magY, fd.magZ};

					float roll = (float)-Math.atan2(a[0], a[2]), 
						 pitch = (float)-Math.atan2(a[1], a[2]), 
						 yaw=0;
					hud.newFlightData(roll, pitch, yaw);
					commsProxy.notifyTitaniumHUD(roll, pitch, yaw);
					
				}
				
			}else if (CommonSettings.isProtocolMAVLink()){
				switch(m.messageType){
					case msg_attitude.MAVLINK_MSG_ID_ATTITUDE:{
						msg_attitude msg = (msg_attitude) m;
						hud.newFlightData(msg.roll, msg.pitch, msg.yaw);
						commsProxy.notifyTitaniumHUD((msg.roll* 180.0 / Math.PI), (msg.pitch * 180.0 / Math.PI), (msg.yaw * 180.0 / Math.PI));
						Log.d("ScoeGcsCommsModule", "in comms calling draw");
						blob = TiBlob.blobFromImage(hud.remoteDrawHUD(hudWidth, hudHeight));
						commsProxy.notifyTitaniumHUDimage();
						break;
					}
					case msg_sys_status.MAVLINK_MSG_ID_SYS_STATUS:{
						msg_sys_status msg = (msg_sys_status) m;
		//TODO			hud.setNavMode(MAVLink.getNav(msg.nav_mode));
						hud.setBatteryRemaining(msg.battery_remaining/10.0);
						hud.setbatteryMVolt(msg.voltage_battery);
						commsProxy.notifyTitaniumStatus(msg.battery_remaining/10.0, msg.voltage_battery, ""); // need to implement nav mode yet
						break;
					}	
					
					case msg_waypoint_current.MAVLINK_MSG_ID_WAYPOINT_CURRENT:{
						//msg_waypoint_current msg = (msg_waypoint_current) m;		
						break;
					}
					
					case msg_gps_raw_int.MAVLINK_MSG_ID_GPS_RAW_INT:{
						msg_gps_raw_int msg = (msg_gps_raw_int) m;
						hud.setAltitude(msg.alt);

						String s;
						
						switch(msg.fix_type){
							case 0:
							case 1:
	                            s="No Fix";
	                            break;
	                            
	                        case 2:
	                        	s="2D Fix";
	                            break;

	                        case 3:
	                        	s="3D Fix";
	                            break;
	                        
	                        default:
	                            s="No GPS";
	                            
						}

						hud.setGPSFix(s);
						
						break;
					}
					
					case msg_gps_status.MAVLINK_MSG_ID_GPS_STATUS:{
						break;
					}
					
					case msg_nav_controller_output.MAVLINK_MSG_ID_NAV_CONTROLLER_OUTPUT:{
						break;
					}					
				}
			}
		}
	};	
	
}
